package com.xbongbong.saas.analytical.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.FundsSetErrorCodeEnum;
import com.xbongbong.saas.analytical.SaasValidateAnalyticalService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.FundAccountEntity;
import com.xbongbong.saas.domain.entity.FundTransferEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.FundTransferEnum;
import com.xbongbong.saas.model.FundAccountModel;
import com.xbongbong.saas.model.FundTransferModel;
import com.xbongbong.util.FundTransferUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/15 16:04
 */
@Service("fundTransferAnalyticalServiceImpl")
public class FundTransferValidateAnalyticalServiceImpl implements SaasValidateAnalyticalService {

    @Resource
    private FundTransferModel fundTransferModel;
    @Resource
    private FundAccountModel fundAccountModel;

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.FUND_TRANSFER.getCode());
    }

    @Override
    public void analyticalData(ValidateDataDTO validateDataDTO) throws XbbException {
        beforeSave(validateDataDTO);
    }
    
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        String corpid = validateDataDTO.getCorpid();
        Long dataId = validateDataDTO.getDataId();
        JSONObject data = validateDataDTO.getData();
        FundTransferEntity fundTransferEntity = null;
        if (Objects.nonNull(dataId)) {
            fundTransferEntity = fundTransferModel.getByKey(validateDataDTO.getDataId(), validateDataDTO.getCorpid());
        }
        if (Objects.isNull(dataId)) {
            String outAccountStr = data.getString(FundTransferEnum.OUT_FUND_ACCOUNT.getAttr());
            String inAccountStr = data.getString(FundTransferEnum.IN_FUND_ACCOUNT.getAttr());
            BigDecimal amount = data.getBigDecimal(FundTransferEnum.AMOUNT.getAttr());
            if (Objects.equals(outAccountStr, inAccountStr)) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500001);
            }
            //调拨时:(1)调拨金额不能为0 (2)资金账户转出账户里面的金额不能小于0  (3)并且转出账户里面得钱不能小于转出金额
            FundAccountEntity fundAccountEntity = fundAccountModel.getByIdCorpid(Long.parseLong(outAccountStr), corpid);
            BigDecimal leftAmount = fundAccountEntity.getAmount();
            if (amount.compareTo(new BigDecimal(BasicConstant.ZERO)) == BasicConstant.ZERO) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500003);
            }
            if ((new BigDecimal(BasicConstant.ZERO)).compareTo(leftAmount) > BasicConstant.ZERO) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500002);
            }
            if (leftAmount.compareTo(amount) < BasicConstant.ZERO) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500004);
            }
            //调拨日期仅支持31天至今
            Long transferTime = data.getLong(FundTransferEnum.TRANSFER_TIME.getAttr());
            Long now = DateTimeUtil.getInt();
            if (transferTime > now) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500005.getCode(), String.format(FundsSetErrorCodeEnum.API_ERROR_2500005.getMsg(), FundTransferEnum.TRANSFER_TIME.getAttrName()));
            }
            Integer count = DateTimeUtil.lastConnect(transferTime, now);
            if (count > TimeConstant.MONTH) {
                throw new XbbException(FundsSetErrorCodeEnum.API_ERROR_2500006.getCode(), String.format(FundsSetErrorCodeEnum.API_ERROR_2500006.getMsg(), TimeConstant.MONTH));
            }
        }else if (Objects.nonNull(validateDataDTO.getDataId()) && Objects.equals(validateDataDTO.getIsImport(), 1)) {
            //覆盖导入的时候需要移除一些不能编辑的系统字段
            FundTransferUtil.removeUnCoverImportValue(data, fundTransferEntity.getData());
        }
    }


}
